# TroubleshooterScript - This script checks for the presence of a root cause
# Key Cmdlets:
# -- update-diagrootcause flags the status of a root cause and can be used to pass parameters
# -- get-diaginput invokes an interactions and returns the response
# -- write-diagprogress displays a progress string to the user

$RootCauseID = "RC_SymbolServerNotFound"

$RootCauseDetected = $false # default to not resolved

$root_path = Get-DiagInput -id "TS_RootSearchPath"

# We have a problem if the specified root_path directory
# doesn't have a "pingme.txt" file and a "000Admin" directory
if (!(Test-Path $root_path\pingme.txt) -or !(Test-Path $root_path\000Admin))
{
    # No symbol server was detected in the specified path... that's a problem
    $search_subdirs = Get-DiagInput -id "TS_SearchSubDirectories"
    if ($search_subdirs -eq "Y")
    {
        # Search all subdirectories for a symbol server
        # If not found, root cause was detected.
        # If found, the root cause was not detected
        # (and we need to let the user know the location we found)

        $found_path = (dir $root_path -r | where { $_.PSIsContainer -and (Test-Path "$($_.FullName)\pingme.txt") -and (Test-Path "$($_.FullName)\000Admin") } |% { $_.FullName })
        
        if (Test-Path $found_path)
        {
            Get-DiagInput -id "TS_FoundSymbolServer" -Parameter @{ "foundpath" = $found_path }
        }
        else
        {
            $RootCauseDetected = $true
        }
    }
    else
    {
        $RootCauseDetected = $true
    }
}

#The following line notifies Windows Troubleshooting Platform of the status of this root cause
update-diagrootcause -id $RootCauseId -detected $RootCauseDetected